// pages/login/login.js
const app = getApp();
const getToken= require('../../utils/get_token.js');
let openId, pg = 0, goodId, tuanId,accountInfo;
var times;
const ckToken = async (that) => {//token确认
  let nTime = Math.floor(new Date().getTime() / 1000);
  let oldTime = wx.getStorageSync('ntime') || 0;
  if (nTime < oldTime)//现在的时间小于到期时间
  {
    lorB(that);
  } else {
    let tokens = await getToken.getToken();
    lorB(that);
  }
}
const lorB = function (that) {//获取图片验证码
  let params = ['common/verify/req/120/50/4', {}]
  getToken.getData(function (res) {
    that.data.yzCode = "data:image/jpeg;base64," + res.data.verifyCodeBase64;
    that.data.yzCodeId = res.data.verifyCodeId;
    that.setData({
      yzCode: that.data.yzCode
    })
  }, ...params)
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:1,
    Num:180,
    codeV:'获取验证码',
    yzCode:'',
    boolT:true,
    loading1:false,
    disabled1:false,
    loading2: false,
    disabled2: false,
    loading3: false,
    disabled3: false,
    getShow:true,
    shareId:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    pg=options.id;
    goodId = options.gid||'';
    tuanId = options.tuanid||'';
    openId = wx.getStorageSync('openId') || '';
    that.data.shareId=wx.getStorageSync('shareId') || '';
    accountInfo = wx.getAccountInfoSync();
    ckToken(that);
  },
  change: function (e) {//点击切换模块
    let that=this;
		let tab = e.currentTarget.dataset.id;
    tab=Number(tab);
    that.setData({
      type: tab
    })
	},
  changeImg: function (e) {//切换图片
    var that = this;
    lorB(that);
  },
  getPhoneNumber(e) {//获取手机号  
    console.log(e)
    let that = this;
    that.setData({
      loading1:true,
      disabled1:true
    })
    getToken.getOpenId(function (res) {
      console.log(res)
      wx.setStorageSync('openId', res.data.openId);
      openId = res.data.openId;
      that.setData({
        loading1: false,
        disabled1: false
      })

      let params = ['gift/messagePush/getWxPhone', { "iv": e.detail.iv, "encryptedData": e.detail.encryptedData, "openId": openId, 'appId':accountInfo.miniProgram.appId,"recommenderId":that.data.shareId}]
      getToken.getData(function (res) {
        console.log(res.data);
        if (res.data.flag) {
          wx.setStorageSync('memberId', res.data.memberId);
          wx.setStorageSync('phone', res.data.mobile);
          wx.setStorageSync('hebiRedFlag', res.data.hebiRedFlag);
          if (pg == 1) {
            wx.switchTab({
              url: '../index/index'
            })
          } else if (pg == 2) {
            wx.redirectTo({
              url: '../paired_detail/paired_detail?scene=' + goodId
            })
          } else if (pg == 3) {
            wx.redirectTo({
              url: '../tuxedo/tuxedo?id=' + goodId + '&tuanid=' + tuanId
            })
          } else if (pg == 4) {
            wx.switchTab({
              url: '../personal/personal'
            })
          }
        }else{
          wx.showToast({title:res.data.message,icon:'none',duration: 2000})        
        }
      }, ...params)
    });
  },
  phoneblur: function (e) {//电话键盘输入事件
    this.data.moboil = e.detail.value;
  },
  imgcodeblur: function (e) {//图片键盘输入事件
    this.data.imgcode = e.detail.value;
  },
	getcode:function(e){//获取验证码
    let that = this;
    let test = /^1(3|4|5|6|7|8|9)\d{9}$/;
    let timestamp = Date.parse(new Date());
    let imei = 'XCX' + timestamp;
    if (that.data.moboil== '') {
      wx.showToast({title:'请输入手机号码',icon:'none',duration: 2000})
			return false;
		}
    if (that.data.imgcode == '') {
      wx.showToast({title:'请输入图片验证码',icon:'none',duration: 2000})
      return false;
    }
    if (!that.data.moboil.match(test)) {
      wx.showToast({title:'手机号格式不正确',icon:'none',duration: 2000})
			return false;
		}
    if (that.data.getShow) {
      that.data.getShow = false;
      let params = [ 'common/sendSms/sendSmsByVerifyCode', {"mobile":that.data.moboil,"MC":86,"imei": imei,"verifyCodeId":that.data.yzCodeId,"code":that.data.imgcode,"type":41}]
      getToken.getData(function (res) {
        console.log(res.data)
        if (!res.data.flag) {//失败
          wx.showToast({title:res.data.message,icon:'none',duration: 2000})
          that.data.getShow = true;
        } else {//成功
          times = setInterval(function () {
            let time = that.data.Num - 1;
            if (time <= 0) {
              clearInterval(times);
              that.data.getShow = true;
              that.setData({
                Num: 180,
                codeV: '获取验证码'
              })
            } else {
              that.setData({
                codeV: time + 's',
                Num: time
              })
            }
          }, 1000)
        }
      }, ...params)
		}
	},
	formSubmit1:function(e){//快捷登录
    let that = this;
    let test = /^1(3|4|5|6|7|8|9)\d{9}$/;
    let val = e.detail.value;
    if (val.moboil == '') {
      wx.showToast({title:'请输入手机号码',icon:'none',duration: 2000})
		 return false;
	 }
    if (!val.moboil.match(test)) {
      wx.showToast({title: '手机号格式不正确',icon:'none',duration: 2000})
		 return false;
	 }
	 if (that.data.boolT) {
		 that.data.boolT = false;
     let params = [ 'common/member/loginBySmsForWeb', {"mobile":val.moboil,"smsCode":val.code,"openId":openId,"recommenderId":that.data.shareId}]
     getToken.getData(function (res) {
       wx.hideLoading();
       setTimeout(function () {
         that.data.boolT = true;
       }, 500)
       if (!res.data.succse || res.data.succse == "false")//登录失败
       {
        wx.showToast({title:res.data.info,icon:'none',duration: 2000})
       } else {
         wx.setStorageSync('memberId', res.data.id);
         wx.setStorageSync('phone', res.data.mobile);
         wx.setStorageSync('hebiRedFlag', res.data.hebiRedFlag);
         if (pg == 1) {
           wx.switchTab({
             url: '../index/index'
           })
         } else if (pg == 2) {
           wx.redirectTo({
             url: '../paired_detail/paired_detail?scene=' + goodId
           })
         } else if (pg == 3) {
           wx.redirectTo({
             url: '../tuxedo/tuxedo?id=' + goodId + '&tuanid=' + tuanId
           })
         } else if (pg == 4) {
           wx.switchTab({
             url: '../personal/personal'
           })
         }
       }
     }, ...params)
	 }
	 
	},
	formSubmit2: function (e) {//账号密码登录
		let that = this;
    let test = /^1(3|4|5|6|7|8|9)\d{9}$/;
    let val = e.detail.value; 
    val.password = val.password.replace(/\s+/g, "");
		if (val.phone == '') {
      wx.showToast({title:'请输入手机号码',icon:'none',duration: 2000})
			return false;
		}
		if (!val.phone.match(test)) {
      wx.showToast({title:'手机号格式不正确',icon:'none',duration: 2000})
			return false;
		}
		if (val.password== '') {
      wx.showToast({title:'请输入密码',icon:'none',duration: 2000})
			return false;
		}
		if (that.data.boolT) {
			that.data.boolT = false;
			that.setData({
        loading3:true,
        disabled3:true
      })
      let params = ['common/member/loginForWeb', {"name":val.phone,"password":val.password,"openId":openId}]
      getToken.getData(function (res) {
        that.setData({
          loading3: false,
          disabled3: false
        })
        setTimeout(function () {
          that.data.boolT = true;
        }, 500)
        console.log(res.data)
        if (!res.data.succse || res.data.succse == "false")//登录失败
        {
          wx.showToast({title:res.data.info,icon:'none',duration: 2000})
        } else {
          wx.setStorageSync('memberId', res.data.id);
          wx.setStorageSync('phone', res.data.mobile);
          wx.setStorageSync('hebiRedFlag', res.data.hebiRedFlag);
          if (pg == 1) {
            wx.switchTab({
              url: '../index/index'
            })
          } else if (pg == 2) {
            wx.redirectTo({
              url: '../paired_detail/paired_detail?scene=' + goodId
            })
          } else if (pg == 3) {
            wx.redirectTo({
              url: '../tuxedo/tuxedo?id=' + goodId + '&tuanid=' + tuanId
            })
          } else if (pg == 4) {
            wx.switchTab({
              url: '../personal/personal'
            })
          }
        }
      }, ...params)
		}
	},
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})